package de.gameduell.framework.gui.util {
	import de.gameduell.framework.util.Library;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 * @author cbra
	 */
	public class ToolTip extends TextBackground {

		private static var _timeline:Sprite;

		private const DEFAULT_MOUSE_OFFSET:int = 15;
		private const DEFAULT_FRAME_OFFSET:int = 6;
		private const DEFAULT_FRAME_OFFSET_VERTICAL:int = 3;
	
		protected var _element:DisplayObject;
		protected var _visible:Boolean;
		protected var _active:Boolean;
		protected var _mouseOffset:int;
		protected var _showTimer:Timer;
		
		/**  
		 * @param libraryID = libraryID of the tooltip clip
		 * @param aId       = the name of the xml tag that holds the text for the toolptip
		 * @param aElement  = the DisplayObject the tooltip is intended for
		 * @param html 	    = decides if the text in the tooltip is displayed as HTML
		**/
		public function ToolTip(libraryID:String,aId:String,aElement:DisplayObject, html:Boolean){
			super(Library.getInstance().createSprite(libraryID), aId);
			id = aId;
			_element = aElement;
			_html = html;
			_visible = false;
			_active = true;
			_mouseOffset = DEFAULT_MOUSE_OFFSET;
			_showTimer = new Timer(300,1);
			_showTimer.addEventListener(TimerEvent.TIMER, show);
			autoSizeVertical = AutoSize.UP;
 			offset = DEFAULT_FRAME_OFFSET;
 			offsetVertical = DEFAULT_FRAME_OFFSET_VERTICAL;
			_element.addEventListener(MouseEvent.MOUSE_OUT, onMouseEvent);
			_element.addEventListener(MouseEvent.MOUSE_OVER, onMouseEvent);		
		}

		/*###################################################
		#		   			  STATIC						#
		#####################################################*/

		public static function init( aTimeline:Sprite ):void {
			_timeline = aTimeline;	
		}

		/*###################################################
		#		   			  PUBLIC						#
		#####################################################*/
		
		override public function setText(aText:String,isHtml:Boolean,aAutoSize:String):void{
			super.setText(aText, isHtml, AutoSize.LEFT);
		}

		public function set active(isActive:Boolean):void {
			_active = isActive;
			if(!_active) {
				_showTimer.stop();	
				hide();
			}else {	
				if(_element.hitTestPoint(_timeline.mouseX, _timeline.mouseY)) {	
					showTimer();
				}
			}
		}

		public function set mouseOffset(aMouseOffset:int):void {
			_mouseOffset = aMouseOffset;
		}

		override public function get visible():Boolean {
			return _visible;
		}

		/*###################################################
		#		   			  INTERNAL						#
		#####################################################*/

		private function showTimer(event:Event = null):void{
			_showTimer.reset();
			_showTimer.start();
		}
		
		public function hide():void {
			_showTimer.stop();
			if(_visible) {
				_visible = false;
				_timeline.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);		
				_timeline.removeChild(sprite);
			}
		}

		public function show(event:Event = null):void {
			if(_element.hitTestPoint(_timeline.mouseX, _timeline.mouseY)) {
				if(!_visible && _active) {
					_visible = true;				
					_timeline.addChild(sprite);
					_timeline.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);	
					calcPos();
					resize();
				}	
			}
		}

		private function onMouseMove(event:MouseEvent):void {
			calcPos();		
			if(!_element.hitTestPoint(_timeline.mouseX, _timeline.mouseY)) {
				hide();
			}			
		}

		private function calcPos():void {			
			if(_timeline.mouseX > _timeline.stage.stageWidth / 2) {
				x = _timeline.mouseX - _bg.width - _mouseOffset;
			}else {
				x = _timeline.mouseX + _mouseOffset;
			}
			
			if(_timeline.mouseY > _timeline.stage.stageHeight / 2) {
				y = _timeline.mouseY - _bg.height - _mouseOffset;
			}else {
				y = _timeline.mouseY + _mouseOffset;
				;
			}
		}

		/**
		 * called on each mouseEvent
		 * 
		 * @param event thrown MouseEvent
		 */
		private function onMouseEvent(event:MouseEvent):void {
			switch(event.type) {
				case MouseEvent.MOUSE_OVER:		
					showTimer();
					break;
				case MouseEvent.MOUSE_OUT:
					_showTimer.stop();
					hide();
					break;
			}
		}
	}
}



